<?php
/**
 * Base_App_Cli initializes Base_App for being accessed via the cli.
 *
 * LICENSE
 *
 * This source file is subject to the new CC-GNU LGPL
 * It is available through the world-wide-web at this URL:
 * http://creativecommons.org/licenses/LGPL/2.1/
 *
 * @category   Base_App
 * @copyright  Copyright (c) 2009 Alexander fanatique* Thomas (me@alexander-thomas.net)
 * @license    http://creativecommons.org/licenses/LGPL/2.1/
 */
class Base_App_Cli extends Base_App_Abstract
{
	private $task;
	private $params;
	
	/**
 	 * Initializes the Base_App as a web application
	 *
	 * @return void
	 * @author Alexander Thomas
	 */
	public function init(){
		return true;
		$init = Base_App_Initialization::getInstance();
		$init->registerApplicationPath($applicationPath);
		$environment = $init->getEnvironment();
		$appConfig = $init->setupApplicationConfig($environment);
		$init->setupLogger();
		$init->setupDb();
		$init->setupAcl();
		$init->setupSession(); 
		$init->setupTranslation();
		$init->getBuildNumber();
		$init->setupHelperPaths();
	}

	/**
	 * Evaluates the parameters that were passed to base_app.php
	 * @see #run()
	 *
	 * @param array $cli_values 
	 * @return void
	 * @author Alexander Thomas
	 */
	public function setParams(array $cli_values){
		$this->task = $cli_values[1];
		unset($cli_values[0], $cli_values[1]);
		//array_merge is for resetting the array keys
		$this->params = array_merge(array(), $cli_values);

	}
	/**
	 * Tries to find the $className in the applications's "Cli_" scope,
	 * tries to find the $className in the "Base_App_" scope,
	 * then @throws Base_App_Cli_Exception
	 *
	 * @return void
	 * @author Alexander Thomas
	 */
	public function run(){
		$this->_run();
		$applicationClassName = "Cli_" . $this->task;
		$baseAppClassName = "Base_App_" . $this->task;
		try {
			$obj = new $baseAppClassName($this->params);
			$obj->run();
		} catch (Exception $e) {
			try {
				$obj = new $applicationClassName($this->params);
				$obj->run();							
			} catch (Exception $e) {
				throw new Base_App_Cli_Exception("\"".$this->task."\" does not match an User Controller nor a Base_App class.");
			}
		}
	}
}
?>
